package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.DriverMaintainBiz;
import com.egao.orderB.entity.DriverMaintain;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2021-05-28 16:37:15
 * @version ：1.0
 */
@Repository
public class DriverMaintainBizImpl extends BaseDao implements DriverMaintainBiz {

	@Override
	public JsonResult list(PageResult2<T> page, DriverMaintain driverMaintain, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<DriverMaintain> rm = new BeanPropertyRowMapper<DriverMaintain>(DriverMaintain.class);
		StringBuffer sql = new StringBuffer("select d.id,d.organization_id,d.driver_id");
        sql.append(",d.driver,d.car_no,d.type,d.m_date");
		sql.append(",d.m_price,d.remark,d.create_time,d.create_user");
		sql.append(",d.update_time,d.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_driver_maintain d ");
        sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=d.organization_id and sogzn.deleted=0 ");
        sql.append(" where d.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and d.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
    	if(Utils.isNotEmpty(driverMaintain)){
			if(Utils.isNotEmpty(driverMaintain.getDriver())){
				sql.append(" and instr(d.driver,:driver)>0 ");
				map.put("driver", driverMaintain.getDriver());
			}
			if(Utils.isNotEmpty(driverMaintain.getCarNo())){
				sql.append(" and instr(d.car_no,:car_no)>0 ");
				map.put("car_no", driverMaintain.getCarNo());
			}
			if(Utils.isNotEmpty(driverMaintain.getSort())&&Utils.isNotEmpty(driverMaintain.getOrder())){
                sql.append(" order by d."+driverMaintain.getSort()+" "+driverMaintain.getOrder());
            } else {
                sql.append(" order by d.create_time desc");
            }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<DriverMaintain> list(DriverMaintain driverMaintain, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<DriverMaintain> rm = new BeanPropertyRowMapper<DriverMaintain>(DriverMaintain.class);
        StringBuffer sql = new StringBuffer("select d.id,d.organization_id,d.driver_id");
        sql.append(",d.driver,d.car_no,d.type,d.m_date");
		sql.append(",d.m_price,d.remark,d.create_time,d.create_user");
		sql.append(",d.update_time,d.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_driver_maintain d ");
        sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=d.organization_id and sogzn.deleted=0 ");
        sql.append(" where d.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and d.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
    	if(Utils.isNotEmpty(driverMaintain)){
			if(Utils.isNotEmpty(driverMaintain.getDriver())){
				sql.append(" and instr(d.driver,:driver)>0 ");
				map.put("driver", driverMaintain.getDriver());
			}
			if(Utils.isNotEmpty(driverMaintain.getCarNo())){
				sql.append(" and instr(d.car_no,:car_no)>0 ");
				map.put("car_no", driverMaintain.getCarNo());
			}
            if(Utils.isNotEmpty(driverMaintain.getSort())&&Utils.isNotEmpty(driverMaintain.getOrder())){
				sql.append(" order by d."+driverMaintain.getSort()+" "+driverMaintain.getOrder());
			} else {
				sql.append(" order by d.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public DriverMaintain getById(DriverMaintain driverMaintain) {
		RowMapper<DriverMaintain> rm = new BeanPropertyRowMapper<DriverMaintain>(DriverMaintain.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select d.id,d.organization_id,d.driver_id");
        sql.append(",d.driver,d.car_no,d.type,d.m_date");
		sql.append(",d.m_price,d.remark,d.create_time,d.create_user");
		sql.append(",d.update_time,d.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_driver_maintain d ");
        sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=d.organization_id and sogzn.deleted=0 ");
        sql.append(" where d.deleted=0 and d.id=:id ");
		map.put("id", driverMaintain.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(DriverMaintain driverMaintain, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_driver_maintain (");
        sql.append("organization_id,driver_id,driver,car_no");
		sql.append(",type,m_date,m_price,remark");
		sql.append(",create_user,create_time) values ( ");
        sql.append(":organization_id,:driver_id,:driver,:car_no");
		sql.append(",:type,:m_date,:m_price,:remark");
		sql.append(",:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("driver_id", driverMaintain.getDriverId()==null?null:driverMaintain.getDriverId()+"");
        map.put("driver", driverMaintain.getDriver());
        map.put("car_no", driverMaintain.getCarNo());
        map.put("type", driverMaintain.getType());
        map.put("m_date", driverMaintain.getMDate());
        map.put("m_price", driverMaintain.getMPrice());
        map.put("remark", driverMaintain.getRemark());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(DriverMaintain driverMaintain, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_driver_maintain set ");
        sql.append("driver_id=:driver_id,driver=:driver,car_no=:car_no ");
		sql.append(",type=:type,m_date=:m_date,m_price=:m_price ");
		sql.append(",remark=:remark,update_time=now(),update_user=:update_user where id=:id ");
        map.put("driver_id", driverMaintain.getDriverId()==null?null:driverMaintain.getDriverId()+"");
        map.put("driver", driverMaintain.getDriver());
        map.put("car_no", driverMaintain.getCarNo());
        map.put("type", driverMaintain.getType());
        map.put("m_date", driverMaintain.getMDate());
        map.put("m_price", driverMaintain.getMPrice());
        map.put("remark", driverMaintain.getRemark());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", driverMaintain.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(DriverMaintain driverMaintain, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(driverMaintain.getId().length()>0){
			for(String ID : driverMaintain.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_driver_maintain set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
